﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Http;
using Golos.Tags.TagCache;

namespace Golos.Tags.Http
{
    public partial class TagCacheServer : HttpServer
    {
        public TagCacheServer(int port): base(port)
        {
            tagsCache = new Cache();
            LogClass.WriteInfoEntry(string.Format("Listening on http://*:{0}/", port));
        }

        protected override void IncomingRequest(HttpListenerContext context, ref int StatusCode)
        {
            string method = context.Request.HttpMethod;
            switch (method.ToUpperInvariant())
            {
                case "GET":
                    Get(context, ref StatusCode);
                    break;
                case "POST":
                    Post(context, ref StatusCode);
                    break;
                case "DELETE":
                    Delete(context, ref StatusCode);
                    break;
                default:
                    break;
            }
        }

        private void Delete(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "removequestiontags":
                    RemoveQuestionTags(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private void Post(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "addtags":
                    AddTags(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private void Get(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "search":
                    SearchQuestion(context, ref statusCode);
                    break;
                case "alltags":
                    GetAllTags(context, ref statusCode);
                    break;
                case "questiontags":
                    GetQuestionTags(context, ref statusCode);
                    break;
                case "populartags":
                    GetPopularTags(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private readonly Cache tagsCache;
    }
}
